<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="org.teammach.laddercomp.dao.LadderManager"%>
<%@ page import="org.teammach.laddercomp.dao.BlogManager"%>

<%@ page import="org.teammach.laddercomp.shared.Ladder"%>
<%@ page import="org.teammach.laddercomp.shared.OfficialLadder"%>
<%@ page import="org.teammach.laddercomp.shared.Team"%>
<%@ page import="org.teammach.laddercomp.shared.BlogEntry"%>

<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="java.text.SimpleDateFormat"%>


<%@ page import="java.util.List"%>
<%@ page import="java.util.TimeZone"%>


<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css">
<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-23998388-1']);
 _gaq.push(['_trackPageview']);

 (function() {
   var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
 })();

</script>
</head>
<body>
<div id="wrapper">

<%
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	LadderManager manager = LadderManager.INSTANCE;
%>
<a href="/">
<span id="header">
</span>
</a>
<div id="page">

<div id="content">
<h1 class="title">Standings</h1>

<table width="90%">
	<tr>
		<th width="5%"> </th>
		<th width="20%">Team name</th>
		<th width="10%">Score</th>
		<th width="65%">Haydn's wise words</th>
	</tr>
	<%
		List<Ladder> ladders = manager.listLadders();
	Ladder official = manager.getLadder("Official");
	for(String team : official.getTeams()) {
		int totalScore = 0;
		int actual = official.getTeams().indexOf(team) + 1;
		for(Ladder ladder : ladders) {
			int predicted = ladder.getTeams().indexOf(team) + 1;
			int difference = Math.abs(actual - predicted);
			totalScore += (5-difference);
			
		}
		%>
		<%= team.toString() %> :  <%=Math.round(totalScore/(ladders.size()-1)) %> <br>
		<%
	}
	
	
	%>

<div>

<div id="sidebar">
<h1 class="title">Latest</h1>



	<div  style="text-align:center;margin-top:-10px;padding:5px">
		<%
			BlogManager blogManager = BlogManager.INSTANCE;
			BlogEntry latest = blogManager.getLatestBlog();
			if (latest == null) {
		%>
		No blog entries have been recorded so far.
		<%
			} else {
				if(latest.getContent().length() < 500) {
		%>
		<a href="blog.jsp"><%=latest.getContent()%></a>
		<%
				}else {
					%>
					<a href="blog.jsp" style="text-decoration:none"><%=latest.getContent().substring(0,497)%>...</a>
					<%
				}
			}
		%>
	</div>
	</div>


<div id="sidebar">
<h1 class="title">Current Ladder</h1>
<table width="90%">
	<tr>
		<th>Team</th>
		<th>P</th>
		<th>W</th>
		<th>L</th>
		<th>D</th>
		<th>PTS</th>
	</tr>
	<%
		int i = 0;
		for (Team team : manager.getTeams()) {
	%>
	<tr<%if (i++ % 2 == 0) {%> class="alt" <%}%>>
		<td><%=team.getTeamName()%></td>
		<td><%=team.getPlayed()%></td>
		<td><%=team.getWon()%></td>
		<td><%=team.getLost()%></td>
		<td><%=team.getDrawn()%></td>
		<td><%=team.getPoints()%></td>
	</tr>
	<%
		}
	%>


	</table>

	</div>
	
	<div id="sidebar">
<h1 class="linksTitle">Links</h1>



	<div  style="text-align:center;margin-top:-10px;padding:5px">
		<ul>
			<li><a href="standings.jsp">Current Standings</a></li>
			<li><a href="rules.jsp">Rules and regulations</a> </li>
			<li><a href="blog.jsp">Haydn's weekly blog</a> </li>
			<li><a href="mailto:dscerri@gmail.com">Contact Webmaster</a> </li>
				<%
		if (user != null
				&& (user.getEmail().equalsIgnoreCase("dscerri@gmail.com") || user
						.getEmail().equalsIgnoreCase("haydns33@gmail.com"))) {
	%>
	 <li><a href="update.jsp">Update ladder</a></li> 
	<li><a href="<%=userService.createLogoutURL(request.getRequestURI())%>">Log out</a>	</li><%
		} else {
	%>
	<li><a href="<%=userService.createLoginURL(request.getRequestURI())%>">Admin login</a></li>
	<%
		}
	%>
		</ul>
	</div>
	</div>

			
	</div>
	</div>
	
	</div>
	<div id="footer">
	<p>&copy;2011 All Rights Reserved &nbsp;&bull;&nbsp; Design by Dave
	Scerri Productions &nbsp;&bull;&nbsp; CSS Template from <a
		href="http://www.freecsstemplates.org/">Free CSS Templates</a> .</p>
	</div>
</body>
</html>